// 字符串拼接方法
const { findSql, insertData, updateSql, removeSql } = require("../DB/joinSql");
// 连接池的查询方法
const query = require("../DB/create_pool").query;
class Model {
  constructor(table, datas) {
    this.table = table;
    this.model = datas;
    // 手动给传过来的数据添加uuid()方法，在数据库中会自动生成zid
    this.model.zid = "REPLACE(UUID(),'-','')";
  }
  /**
   *
   * @param {Object} where 查询条件
   */
  static find(table, where) {
    if (!where) return query(findSql(table));
    return query(findSql(table, where));
  }
  save() {
    return query(insertData(this.table, this.model));
  } 
  static update(table, sets, where) {
    return query(updateSql(table, sets, where));
  }
  static remove(table, where) {
    return query(removeSql(table, where));
  }
}

//设置方法，只提供查询服务
Model.setTable = function(table) {
  return new Model(table, {});
};

module.exports = Model;

/**
 *  new Model(table,datas:{name:'xu',age:19}) > model
 *  model.save() > mysql
 *  model.find(where?).then()
 *  model.update(sets,where).then()
 *  model.remove(where).then()
 */
